package com.lollipop.programming.thread.sequential;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author Lollipop
 * @Date 2025/7/28 15:27
 * @Description 多线程顺序执行 - 单线程池Demo
 * @Version 1.0
 */
@Slf4j
public class SingleThreadExecutor {
    public static void main(String[] args) {
        // 创建单线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10));

        // 提交任务
        threadPoolExecutor.execute(() -> {
            log.info("任务T1执行");
        });
        threadPoolExecutor.execute(() -> {
            log.info("任务T2执行");
        });
        threadPoolExecutor.execute(() -> {
            log.info("任务T3执行");
        });

        // 关闭线程池
        threadPoolExecutor.shutdown();
        while (!threadPoolExecutor.isTerminated()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                log.error("线程池关闭异常：{}", e.getMessage());
            }
        }
        log.info("线程池已关闭");
    }

}
